<template>
	<view :style="{ 'background-image': `linear-gradient(0deg, rgba(0, 0, 0, 0.2) 0%, rgba(0, 0, 0, 0.8) 40%),url(${store.mer_banner})` }"
	 class="store-home">
			<view class="mainHead">
				<!-- 店铺信息 -->
				<view id="store" class="store">
					<image :src="store.mer_avatar"></image>
					<view class="text">
						<navigator :url="`/pages/store/detail/index?id=${id}`" hover-class="none">
							<text class="name">{{ store.mer_name }}</text>
						</navigator>
						<view class="score">
							<view class="star">
								<view :style="{ width: `${score.star.toFixed(2)}%` }"></view>
							</view>
							<view>{{ score.number.toFixed(1) }}</view>
						</view>
					</view>
				</view>
				
				<view class="nav">
					<view class="nav-cont">
						<view :class="{ active: navActive === 0 }" class="item" @click="navActive = 0;select.show = !select.show">
							<view class="cont">
								{{ select.selected ? '评分' : '默认' }}
								<text :class="['arrow-icon', 'iconfont', select.show ? 'icon-xiangshang' : 'icon-xiangxia']"></text>
							</view>
						</view>
						<view :class="{ active: navActive === 1 }" class="item" @click="set_where(2)">
							<view class="cont">
								销量
							</view>
						</view>
						<view :class="{ active: navActive === 2 }" class="item" @click="set_where(3)">
							<view class="cont">
								价格
								<image :src="sortPrice ? '/static/images/up.png' : '/static/images/down.png'"></image>
							</view>
						</view>
						<view :class="{ active: navActive === 3 }" class="item" @click="set_where(4)">
							<view class="cont">
								新品
							</view>
						</view>
						<view :class="{ active: navActive === 4 }" class="item" @click="select.show = false;navActive = 4;isColumn = !isColumn">
							<view class="cont">
								<text :class="['layout-icon', 'iconfont', isColumn ? 'icon-pailie' : 'icon-tupianpailie']"></text>
							</view>
						</view>
					</view>
					<view v-show="select.show && !navShow" class="select">
						<view v-for="item in select.options" :key="item.id" :class="{ active: item.id === select.selected }" class="item"
						 @click="set_where(item.id)">{{ item.name }}</view>
					</view>
				</view>
			</view>
			<scroll-view class="main" :style="'height:' + wh + 'px'" scroll-y="true" @scrolltolower="uploadMissioc">
				<view class="tab-cont">
					<!-- 首页 -->
					<view>
						<!-- 商品 -->
						<view class="goods-wrap">
							<view v-if="goods.length" :class="{ column: isColumn }" class="goods">
								<view v-for="(item,index) in goods" :key="index" class="item" @click="goGoodsDetail(item.product_id)">
									<view class="image">
										<image :src="item.image"></image>
									</view>
									<view class="text">
										<view class="name">{{ item.store_name }}</view>
										<view class="money-wrap">
											<view class="money">
												¥
												<text>{{ item.price }}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="over_line" v-if="isover">----- 我也是有底线的 ------</view>
						</view>
					</view>
				</view>
				<view v-if="!goods.length" class="notAvailable">暂无商品~</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		filters: {
			dateFormat: function(value) {
				if (!value) {
					return '';
				}
				return value.split(' ')[0].replace('-', '.');
			}
		},
		data() {
			return {
				systemInfo: uni.getSystemInfoSync(),
				// #ifdef MP
				menuButtonInfo: uni.getMenuButtonBoundingClientRect(),
				// #endif
				id: 0, // 商铺id
				store: {}, // 商铺详情
				goods: [], // 商铺商品
				isColumn: true, // 商品列表排列方式
				navShow: false,
				navActive: 0,
				keyword: '',
				order: '',
				sortPrice: true, // 价格排序
				categoryLoading: false,
				// 下拉菜单
				select: {
					show: false,
					selected: 0,
					options: [{
							id: 0,
							name: '默认'
						},
						{
							id: 1,
							name: '评分'
						}
					]
				},
				storeScroll: true,
				storeTop: 0,
				navHeight: 0,
				wh:'',
				pagenum: 1, // 页码值
				pagesize: 10, // 每页显示多少条数据
				total: '',
				isover: false, // 判断是否到底部，给用户提示
				isloading: false // 判断数据是否在加载中
			}
		},
		computed: {
			score: function() {
				let store = this.store,
					score = {
						star: 0,
						number: 0
					};
				if ('postage_score' in store) {
					score.number = (parseFloat(store.postage_score) + parseFloat(store.product_score) + parseFloat(store.service_score)) /
						3;
					score.star = score.number / 5 * 100;
				}
				return score;
			},
		},
		watch: {
			order: function() {
				this.goods = []
				this.getGoods();
			}
		},
		onLoad: function(options) {
			// 获取设备的高度
			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					that.wh = res.windowHeight - 91
				}
			});
			that.id = options.id;
		},
		onShow(){
			this.getStore();
			this.getGoods();
		},
		mounted: function(){
			const query = uni.createSelectorQuery().in(this);
			query.select('#store').boundingClientRect(data => {
			  this.storeHeight = data.height;
			  this.storeTop = data.top;
			}).exec();
		},
		methods: {
			// 获取商品详情
			getStore() {
				this.$api.getStoreDetail(this.id,res => {
					this.store = res.data;
				})
			},
			// 获取商铺商品
			getGoods() {
				// 正在加载下页数据
				this.isloading = true;
				this.$api.getStoreGoods(this.id, {
					keyword: this.keyword,
					order: this.order,
					page: this.pagenum,
					limit: this.pagesize
				},res => {
					this.total = res.data.count;
					this.goods = [...this.goods,...res.data.list]
					// 没有在加载下页数据
					this.isloading = false;
				})
			},
			// 选择条件展示商品
			set_where(param) {
				this.pagenum = 1
				this.pagesize = 10
				this.select.show = false;
				switch (param) {
					case 0:
						this.select.selected = 0;
						this.order = '';
						break;
					case 1:
						this.select.selected = 1;
						this.order = 'rate';
						break;
					case 2:
						this.navActive = 1;
						this.order = 'sales';
						break;
					case 3:
						this.navActive = 2;
						this.sortPrice = !this.sortPrice;
						this.order = this.sortPrice ? 'price_asc' : 'price_desc';
						break;
					case 4:
						this.navActive = 3;
						this.order = 'is_new';
						break;
				}
			},
			// 去商品详情页
			goGoodsDetail(id) {
				uni.navigateTo({
					url: `/pages/goods_details/index?id=${id}`
				})
			},
			uploadMissioc() {
				if (!this.goods.length) {
					return;
				}
				// 判断当前是否正在请求数据中
				if (this.isloading) {
					return;
				}
				if (this.pagesize * this.pagenum >= this.total) {
					this.isover = true;
					return;
				}
				this.pagenum++;
				this.getGoods();
			}
		}
	}
</script>

<style lang="scss">
	.store-home {
		display: flex;
		flex-direction: column;
		background: left top/750rpx 360rpx no-repeat fixed;
		overflow: hidden;
	}

	.main {
		flex: 1;
		min-height: 0rpx;
		.over_line {
			text-align: center;
			background-color: #F5F5F5;
			font-size: 26rpx;
			color: #999;
			padding: 20rpx 0;
		}
		.notAvailable {
			width: 100%;
			height: 200rpx;
			text-align: center;
			padding-top: 200rpx;
			font-size: 24rpx;
			color: #666;
		}
	}

	.store {
		position: relative;
		z-index: 6;
		display: flex;
		align-items: center;
		padding-right: 20rpx;
		padding-left: 20rpx;
		padding-top: 20rpx;

		image {
			width: 74rpx;
			height: 74rpx;
			border-radius: 6rpx;
		}

		.text {
			flex: 1;
			min-width: 0;
			margin-right: 20rpx;
			margin-left: 20rpx;

			navigator {
				display: inline-flex;
				align-items: center;
				max-width: 100%;

				.name {
					flex: 1;
					min-width: 0;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-weight: bold;
					font-size: 30rpx;
					line-height: 1;
					color: #FFFFFF;
				}

				.iconfont {
					margin-left: 10rpx;
					font-size: 17rpx;
					color: #FFFFFF;
				}
			}

			.score {
				display: flex;
				align-items: center;
				margin-top: 17rpx;
				font-weight: 500;
				font-size: 24rpx;
				line-height: 1;
				color: #FFFFFF;

				.star {
					position: relative;
					width: 111rpx;
					height: 19rpx;
					margin-right: 10rpx;
					background: url(../../../static/images/star.png) left top/100% 100% no-repeat;
					overflow: hidden;

					view {
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						background: url(../../../static/images/star_active.png) left top/111rpx 19rpx no-repeat;
					}
				}
			}
		}

		button {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 113rpx;
			height: 48rpx;
			border-radius: 24rpx;
			background-image: linear-gradient(-90deg, rgba(246, 122, 56, 1) 0%, rgba(241, 27, 9, 1) 100%);
			font-weight: 500;
			font-size: 22rpx;
			color: #FFFFFF;

			.iconfont {
				margin-right: 6rpx;
				font-size: 22rpx;
			}
			&.gary{
				background-color: #999;
			}
		}
	}
	
	.nav.fixed {
		position: fixed;
		left: 0;
		width: 100%;
		.nav-cont{
			position: absolute;
			width: 100%;
		}
	}

	.nav {
		position: relative;

		.nav-cont {
			display: flex;
			align-items: center;
			height: 84rpx;

			.item {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				min-width: 0;

				.cont {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 116rpx;
					height: 44rpx;
					border-radius: 22rpx;
					font-weight: 500;
					font-size: 24rpx;
					color: #FFFFFF;

					.arrow-icon {
						margin-left: 10rpx;
						font-size: 18rpx;
					}

					.layout-icon {
						font-size: 32rpx;
					}

					.icon-pailie {
						font-size: 32rpx;
					}

					image {
						width: 15rpx;
						height: 21rpx;
						margin-left: 7rpx;
					}
				}
			}

			.active {
				.cont {
					background-color: #FFFFFF;
					font-weight: bold;
					color: $theme-color;
				}
			}
		}

		.select {
			position: absolute;
			top: 100%;
			left: 0;
			z-index: 2;
			width: 100%;
			padding-right: 40rpx;
			padding-bottom: 28rpx;
			padding-left: 74rpx;
			border-bottom-right-radius: 24rpx;
			border-bottom-left-radius: 24rpx;
			background-color: #FFFFFF;

			.item {
				margin-top: 28rpx;
				font-size: 24rpx;
				color: #454545;
			}

			.active {
				background: url(../../../static/images/active.png) right center/20rpx no-repeat;
				color: #E93323;
			}
		}
	}

	.goods {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding-top: 20rpx;
		padding-right: 20rpx;
		padding-left: 20rpx;
		background-color: #F5F5F5;

		.item {
			width: 345rpx;
			border-radius: 16rpx;
			margin-bottom: 20rpx;
			background-color: #FFFFFF;
			overflow: hidden;
			.image {
				width: 345rpx;
				height: 345rpx;
				image {
					display: block;
					width: 100%;
					height: 100%;
				}
			}

			.text {
				padding: 20rpx 20rpx 25rpx;
				.name {
					// overflow: hidden;
					// white-space: nowrap;
					// text-overflow: ellipsis;
					font-weight: 500;
					font-size: 30rpx;
					line-height: 1;
					color: #222222;
				}

				.money-wrap {
					display: flex;
					align-items: center;
					margin-top: 43rpx;

					.money {
						font-weight: bold;
						font-size: 26rpx;
						color: $theme-color;
						text {
							font-size: 34rpx;
							line-height: 1;
						}
					}

					.ticket {
						height: 26rpx;
						padding-right: 9rpx;
						padding-left: 9rpx;
						border: 1rpx solid $theme-color;
						border-radius: 4rpx;
						margin-left: 10rpx;
						font-weight: 500;
						font-size: 20rpx;
						line-height: 24rpx;
						color: $theme-color;
					}
				}
				.score {
					margin-top: 20rpx;
					font-weight: 500;
					font-size: 20rpx;
					line-height: 1;
					color: #737373;
				}
			}

			.foot {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 52rpx;
				background: linear-gradient(to right, #F11B09, #F67A38);
				font-weight: 500;
				font-size: 24rpx;
				color: #FFFFFF;
				.iconfont {
					margin-right: 10rpx;
					font-size: 22rpx;
					line-height: 1;
				}
			}
		}
	}

	.column {
		padding: 0;
		background-color: #FFFFFF;
		.item {
			position: relative;
			display: flex;
			align-items: center;
			width: 100%;
			padding: 30rpx 20rpx;
			border-radius: 0;
			margin-bottom: 0;
			&::before {
				content: " ";
				position: absolute;
				top: 0;
				right: 20rpx;
				left: 250rpx;
				border-top: 1rpx solid #F5F5F5;
			}
			.image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 16rpx;
				overflow: hidden;
			}
			.text {
				position: relative;
				flex: 1;
				min-width: 0;
				padding-top: 0;
				padding-right: 0;
				padding-bottom: 0;
				.name {
					color: #282828;
				}
				.money-wrap {
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					margin-top: 52rpx;
					.ticket {
						height: 28rpx;
						padding-right: 12rpx;
						padding-left: 12rpx;
						border: none;
						border-radius: 0;
						margin-top: 17rpx;
						margin-left: 0;
						background: url(../../../static/images/yh.png) top left/100% 100% no-repeat;
						line-height: 28rpx;
					}
				}
			}

			.foot {
				position: absolute;
				right: 20rpx;
				bottom: 30rpx;
				height: 44rpx;
				padding-right: 17rpx;
				padding-left: 17rpx;
				border-radius: 22rpx;
				font-size: 22rpx;
				color: #F5F5F5;
			}
		}
	}

	.category {
		padding-top: 34rpx;
		padding-right: 20rpx;
		padding-left: 20rpx;
		.section {
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			background-color: #FFFFFF;

			.head {
				position: relative;
				display: flex;
				align-items: center;
				height: 90rpx;
				padding-right: 20rpx;
				padding-left: 36rpx;
				font-weight: bold;
				color: #282828;
				&::before {
					content: " ";
					position: absolute;
					top: 50%;
					left: 20rpx;
					width: 6rpx;
					height: 24rpx;
					background-color: $theme-color;
					transform: translateY(-50%);
				}
				.title {
					flex: 1;
					min-width: 0;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 30rpx;
				}
				.iconfont {
					font-size: 22rpx;
					line-height: 1;
				}
			}
			.body {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				align-items: center;
				padding: 9rpx 36rpx 14rpx;
				.item {
					width: 314rpx;
					height: 84rpx;
					padding-right: 30rpx;
					padding-left: 30rpx;
					border-radius: 10rpx;
					background-color: #F5F5F5;
					margin-bottom: 10rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-weight: 500;
					font-size: 26rpx;
					line-height: 84rpx;
					color: #282828;
				}
			}
		}
	}

	.coupon {
		padding: 30rpx;
		margin-top: 34rpx;
		background-color: #F5F5F5;
		.item {
			display: flex;
			margin-bottom: 16rpx;
			.left {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 240rpx;
				background: url(../static/images/coupon1.png) left top/100% 100% no-repeat;
				font-weight: 500;
				font-size: 24rpx;
				line-height: 1;
				color: #FFFFFF;
				&.gary{
					background-image:url('');
				}
				.money {
					margin-bottom: 25rpx;
					font-weight: 800;
					font-size: 36rpx;
					text {
						margin-left: 10rpx;
						font-size: 60rpx;
					}
				}
			}

			.right {
				flex: 1;
				min-width: 0;
				padding-right: 18rpx;
				padding-left: 27rpx;
				background-color: #FFFFFF;
				.name {
					padding-top: 32rpx;
					padding-bottom: 32rpx;
					border-bottom: 1rpx solid #F0F0F0;
					font-weight: 500;
					font-size: 30;
					line-height: 1;
					color: #282828;
					text {
						display: inline-block;
						width: 84rpx;
						height: 28rpx;
						border: 1rpx solid $theme-color;
						border-radius: 14rpx;
						margin-right: 15rpx;
						background-color: #FFF4F3;
						font-weight: 500;
						font-size: 20rpx;
						line-height: 26rpx;
						text-align: center;
						color: $theme-color;
						&.gary{
							border-color: #BBB;
							color: #bbb;
							background-color: #F5F5F5;
						}	
					}
				}

				.time-wrap {
					display: flex;
					align-items: center;
					padding-top: 16rpx;
					padding-bottom: 16rpx;
					font-weight: 500;
					font-size: 20rpx;
					color: #999999;
					.time {
						flex: 1;
						min-width: 0;
					}

					.button {
						width: 136rpx;
						height: 44rpx;
						border-radius: 22rpx;
						background-color: $theme-color;
						font-weight: 500;
						font-size: 22rpx;
						line-height: 44rpx;
						text-align: center;
						color: #FFFFFF;
						&.gary{
							background-color: #999;
						}
					}
				}
			}
		}

		.disabled {
			.left {
				background-image: url(../static/images/coupon2.png);
			}

			.right {
				.name {
					text {
						border-color: #C1C1C1;
						color: #C1C1C1;
					}
				}

				.time-wrap {
					.button {
						background-color: #CCCCCC;
						color: #FFFFFF;
					}
				}
			}
		}
	}
</style>
